package com.longcai.service.fuyoupay.impl;



import java.util.Date;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longcai.bean.ApiResult;
import com.longcai.entity.nearby.ClyjNearbyShops;
import com.longcai.entity.payment.fuyou.request.ClyjFuyouCityNum;
import com.longcai.entity.payment.fuyou.request.ClyjFuyouCompanyRegister;
import com.longcai.fenzhuang.fuyou.BaseResponse;
import com.longcai.fenzhuang.fuyou.FuyouCommonPortMethod;
import com.longcai.mapper.fuyoupay.ClyjFuyouCityNumMapper;
import com.longcai.mapper.fuyoupay.ClyjFuyouCompanyRegisterMapper;
import com.longcai.service.fuyoupay.ClyjFuyouCompanyRegisterService;

/**
 * <p>
 * 富友支付企业注册表 服务实现类
 * </p>
 *
 * @author ZJ
 * @since 2019-03-31
 */
@Service
public class ClyjFuyouCompanyRegisterServiceImpl extends ServiceImpl<ClyjFuyouCompanyRegisterMapper, ClyjFuyouCompanyRegister> implements ClyjFuyouCompanyRegisterService {
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private ClyjFuyouCompanyRegisterService clyjFuyouCompanyRegisterService;
	@Autowired
	private ClyjFuyouCityNumMapper clyjFuyouCityNumMapper;
	@Override
	public ApiResult<ClyjFuyouCompanyRegister> check(ClyjNearbyShops clyjNearbyShops) {
		if(
				StringUtils.isEmpty(clyjNearbyShops.getShopsId())
				||StringUtils.isEmpty(clyjNearbyShops.getShopsName())
				||StringUtils.isEmpty(clyjNearbyShops.getAptitudeTime())
				||StringUtils.isEmpty(clyjNearbyShops.getCorporateName())
				||StringUtils.isEmpty(clyjNearbyShops.getCorporateCard())
				||StringUtils.isEmpty(clyjNearbyShops.getBankPhone())
				||StringUtils.isEmpty(clyjNearbyShops.getEmail())
				||StringUtils.isEmpty(clyjNearbyShops.getThreeCardPic())
				||StringUtils.isEmpty(clyjNearbyShops.getCorporateCardPic())
				||StringUtils.isEmpty(clyjNearbyShops.getBankLicense())
				||StringUtils.isEmpty(clyjNearbyShops.getParentBankId())
				||StringUtils.isEmpty(clyjNearbyShops.getCorporateBankName())
				||StringUtils.isEmpty(clyjNearbyShops.getCorporateBankCard())){
			return ApiResult.fail("参数不能为空");
		};
		ClyjFuyouCompanyRegister cfcr = new ClyjFuyouCompanyRegister();

		cfcr.setId(UUID.randomUUID().toString().replace("-", ""));
		cfcr.setCustName(clyjNearbyShops.getShopsName());
		cfcr.setBrandName(clyjNearbyShops.getShopsName());
		cfcr.setUnifiedCode(clyjNearbyShops.getUnifiedCode());
		cfcr.setBusinessLicen(clyjNearbyShops.getBusinessLicense());
		cfcr.setLicenseIndate(clyjNearbyShops.getAptitudeTime());
		cfcr.setTaxNum(clyjNearbyShops.getTaxNum());
		cfcr.setCompanyOrgNum(clyjNearbyShops.getCompanyOrgNum());
		cfcr.setArtifNm(clyjNearbyShops.getCorporateName());
		cfcr.setContact(clyjNearbyShops.getCorporateName());
		cfcr.setCertifId(clyjNearbyShops.getCorporateCard());
		cfcr.setMobileNo(clyjNearbyShops.getBankPhone());
		cfcr.setEmail(clyjNearbyShops.getEmail());
		cfcr.setBankLicense(clyjNearbyShops.getBankLicense());

		/**根据所在城市查询开户行地区代码CityId*/
		QueryWrapper<ClyjFuyouCityNum> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("city",clyjNearbyShops.getShopCity());
		ClyjFuyouCityNum cityNum = clyjFuyouCityNumMapper.selectOne(queryWrapper);
		cfcr.setCityId(cityNum.getCityNum());

		cfcr.setParentBankId(clyjNearbyShops.getParentBankId());
		cfcr.setBankNm(clyjNearbyShops.getCorporateBankName());
		cfcr.setCapacntNo(clyjNearbyShops.getCorporateBankCard());
		cfcr.setThreeCertificates(clyjNearbyShops.getThreeCardPic());
		cfcr.setAccessory2(clyjNearbyShops.getCorporateCardPic());
		cfcr.setAgreement("1");
		cfcr.setCreateTime(new Date());
		FuyouCommonPortMethod fcpm = new FuyouCommonPortMethod();
		try {
			BaseResponse r = fcpm.CommonRegister(cfcr);
			if(r.getRespCode().equals("0000")){
				cfcr.setLoginId(r.getLoginId());
				return ApiResult.success(cfcr);
			}

		} catch (Exception e) {
			logger.error("注册失败");
			e.printStackTrace();
		}
		return null;
	}
}